#include "WOP_Heap_Entry.h"
#include "Command_Agent.h"
#include "WOPM.h"
//這個include WOPM是經由VC ++給予的"新增include目錄"功能，把前方的路徑省略
//所以如果換其他的IDE發現路徑錯誤，請自行更改路徑。
//元件原位址:"wopm/WOPM.h"
#include <iostream>
#include <cassert>
using namespace std;


void WOP_Heap_Entry::init(string name, WOP_Entry *parent){
	_top = false;
	_parent = parent;
	//_element = new Vector();
	_name = name;
	_value = "" ;
	_updated = true;
	_unfolded = false ;
	_descentfrom = NULL ;
	// VM = NULL;
}
WOP_Heap_Entry::~WOP_Heap_Entry(){
	WOPM* wop = WOPM::getInstance();
	/*
	for(int i =0;i<this->_element->size();i++){
		en = (WOP_Entry*)_element->get(i);
		wop->removeEntry(en);		
	}
	delete _element;
	*/
}
void WOP_Heap_Entry::setDescentFrom(WOP_Entry * from)  {
		if (from != NULL) {                                    // this wop is unfolded from other reference VM
       	                                                                           // we copy its _descentfrom
			if (from->isTop())  
		 			_descentfrom = from ;
		    else  
					_descentfrom = from->getDescentFrom();
			// set the DescentFrom to all the children
			std::vector<WOP_Entry*>::iterator itr;
			for (itr = this->_element.begin(); itr != this->_element.end(); itr++)		{
					(*itr)->setDescentFrom(_descentfrom);
			}
		
		}
}
string WOP_Heap_Entry::getActualPath(){
	//assert(_updated);
	assert(false);
	return "";
}
bool WOP_Heap_Entry::update(){//因為更新必定是由一個Ref_Entry呼叫，我們在此取得該Ref當時的path

	
	if(_updated)
		return _changed;	

	std::vector<WOP_Entry*>::iterator itr;

	bool changed = false ;
	for (itr = this->_element.begin(); itr != this->_element.end(); itr++)
	{
		bool childchange = (*itr)->update();
		if (childchange) changed = true ;

	}	
	if (changed)  _changed = true;
	_updated = true ;
	return _changed ;
	
}
bool WOP_Heap_Entry::update(string path){//因為更新必定是由一個Ref_Entry呼叫，我們在此取得該Ref當時的path

	assert(false); // the following code is old and I do know when this method will be called
	assert(path.size()>0);
	//cout << "in" << endl;
	//如果已經更新，那就跳過
	if(_updated)
		return _updated;	
	assert(false);
	// _refpath = path;	
	_updated = true;
	/*
	自己更新完(更新_recpath的最近紀錄)，再更新child
	*/
	std::vector<WOP_Entry*>::iterator itr;
	//cout << *(this->_elementegin()) << endl;
	//cout << *(this->_element.begin()) << endl;
	for (itr = this->_element.begin(); itr != this->_element.end(); itr++)
	{
		(*itr)->update();
	}
	return _updated ;	
}
void WOP_Heap_Entry::addChild(WOP_Entry* en){
	this->_element.push_back(en);
	//_element->add(en);
}

WOP_Entry* WOP_Heap_Entry::findMatchedWOP(std::string vartype, std::string name) {
	vector<WOP_Entry*>::iterator itr ;

	if (_vartype == vartype) return this ; // this is a heap wop
	                                       // we are not interested in its name
	                                       // for example, a bt object may have name
	                                       // root, root.left, root.right.
	                                       // In the case of array dimension where a array wop
	                                       // participates the mapping tree
	                                       // if we compare name of the heap wop and top wop
	                                       // a MDS cannot be applied to other object 

	for (itr = _element.begin(); itr != _element.end(); itr++) {
		WOP_Entry * p = (*itr);
		WOP_Entry * result = p->findMatchedWOP(vartype,name) ;// a recursive call deep down to each attribute 
		                                                      // of this heap object.
		if (result != NULL) return result ;
	}
	return NULL ;
}
